using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Design;
using System.ComponentModel.Design;

namespace TinyERP.Framework.Controls
{
	/// <summary>
	/// Summary description for MultiColumnComboBox.
	/// </summary>
	public delegate void AfterSelectEventHandler();
	public class MultiColumnComboBox : System.Windows.Forms.ComboBox
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

        private MultiColumnComboPopup popup = new MultiColumnComboPopup();
        private DataGridViewColumnCollection displayColumn;
		
		public event AfterSelectEventHandler AfterSelectEvent;

		public MultiColumnComboBox(System.ComponentModel.IContainer container)
		{
			/// <summary>
			/// Required for Windows.Forms Class Composition Designer support
			/// </summary>
			container.Add(this);
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		public MultiColumnComboBox()
		{
			InitializeComponent();
		}

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
		}
		#endregion
       
        
        private void ShowDropDownForm()
        {
           
            popup.AfterRowSelectEvent += new AfterRowSelectEventHandler(MultiColumnComboBox_AfterSelectEvent);
            
            popup.Location = this.PointToScreen(new Point(0, this.Height));
            popup.Show();
            
            popup.DataSource = this.DataSource;
            if (popup.SelectedItem != null)
            {
                try
                {
                    this.SelectedItem = popup.SelectedItem;
                }
                catch (Exception e2)
                {
                    MessageBox.Show(e2.Message, "Error");
                }
            }
        }
		protected override void OnDropDown(System.EventArgs e){
			Form parent = this.FindForm();
			if(this.DataSource != null){
                ShowDropDownForm();
				if(AfterSelectEvent!=null)
					AfterSelectEvent();
			}
			base.OnDropDown(e);
		}

		private void MultiColumnComboBox_AfterSelectEvent(object sender, object selectedItem){
			try{
                if (selectedItem != null)
                {
                    this.SelectedItem = selectedItem;
				}
			}catch(Exception exp){
				MessageBox.Show(this,exp.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
			}
		}
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(CustomGridColumnEditor),
            typeof(System.Drawing.Design.UITypeEditor)), MergableProperty(false)]
        public DataGridViewColumnCollection DisplayColumns
        {
            get
            {
                return this.popup.DataGridView.Columns;
            }
        }
		

	
		

        public static bool IsDropDownMessage(System.Windows.Forms.Message m)
        {
            bool dropDownMessage = m.Msg == 0x201 || m.Msg == 0x203;    //WM_MOUSEDOWN or WM_LBUTTONDBLCLK
            if (!dropDownMessage && m.Msg == 0x104) // WM_SYSKEYDOWN
            {
                int wparam = m.WParam.ToInt32();
                int lparam = m.LParam.ToInt32();
                if ((wparam == 0x28 && (lparam == 0x21500001 || lparam == 0x20500001)) ||    //Alt+Down, Alt+NumDown
                    (wparam == 0x26 && (lparam == 0x21480001 || lparam == 0x20480001)))        //Alt+Up, Alt+NumUp
                {
                    dropDownMessage = true;
                }
            }
            return dropDownMessage;
        }

        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            bool dropDownMessage = IsDropDownMessage(m);
            if (dropDownMessage)
            {
                ShowDropDownForm();
                return;
            }
            base.WndProc(ref m);
            
        }
	}

    public class CustomGridColumnEditor : CollectionEditor
    {
        public CustomGridColumnEditor(Type type)
            : base(type)
        {
        }
        protected override bool CanSelectMultipleInstances()
        {
            return false;
        }
        protected override Type[] CreateNewItemTypes()
        {
           Type[]  types = new Type[] {typeof(DataGridViewTextBoxColumn),
                                    typeof(DataGridViewCheckBoxColumn), 
                                    typeof(DataGridViewComboBoxColumn),
                                    typeof(DataGridViewImageColumn),
                                    typeof(DataGridViewLinkColumn)};

            return types;
        }
        protected override object CreateInstance(Type ItemType)
        {
            object instance = base.CreateInstance(ItemType);
            if (instance != null)
            {
                ((DataGridViewColumn)instance).ReadOnly = true;
            }
            return instance;
        }
        
        protected override void DestroyInstance(object instance)
        {
            //do some cleaning here
            
            base.DestroyInstance(instance);
        }
    }

}
